package com.example.app3;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgreement;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbAgreement = findViewById(R.id.cb_agreement);
        btnLogin = findViewById(R.id.btn_login);
        setLoginButtonState(false);

        etUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    etUsername.setBackgroundColor(Color.parseColor("#E3F2FD"));
                } else {
                    etUsername.setBackgroundColor(Color.TRANSPARENT);
                    String text = etUsername.getText().toString().trim();
                    etUsername.setText(text);
                }
            }
        });

        etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    etPassword.setBackgroundColor(Color.parseColor("#E3F2FD"));
                } else {
                    etPassword.setBackgroundColor(Color.TRANSPARENT);
                }
            }
        });

        cbAgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setLoginButtonState(isChecked);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString();

                if (username.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!cbAgreement.isChecked()) {
                    Toast.makeText(LoginActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
                    return;
                }

                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "跳转到注册页", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setLoginButtonState(boolean isAgreed) {
        if (isAgreed) {
            btnLogin.setBackgroundColor(Color.parseColor("#6200EE")); // 紫色
            btnLogin.setTextColor(Color.WHITE);
        } else {
            btnLogin.setBackgroundColor(Color.parseColor("#CCCCCC")); // 灰色
            btnLogin.setTextColor(Color.parseColor("#666666"));
        }
    }
}